package com.lanxin.judge.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lanxin.judge.entity.LcFlow;
import com.lanxin.judge.entity.LcNode;
import com.lanxin.judge.mapper.LcFlowMapper;
import com.lanxin.judge.mapper.LcNodeMapper;
import com.lanxin.judge.service.ILcFlowService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lanxin.judge.service.ILcNodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2025-08-01
 */
@Service
public class LcFlowServiceImpl extends ServiceImpl<LcFlowMapper, LcFlow> implements ILcFlowService {

    @Autowired
    private LcFlowMapper flowMapper;

    @Autowired
    private LcNodeMapper nodeMapper;

    //根据条件分页列表审批流程
    @Override
    public IPage<LcFlow> pageJudgeFlow(Integer page,Integer pageSize, String flowName, String docType) {
        QueryWrapper<LcFlow> queryWrapper = new QueryWrapper<>();
        if(page==null||page<1){
            page=1;
        }
        if(pageSize==null||pageSize<5){
            pageSize=5;
        }
        if(flowName!=null&&!flowName.isEmpty()){
            queryWrapper.lambda().like(LcFlow::getFlowName,flowName);
        }
        if(docType!=null&&!docType.isEmpty()){
            queryWrapper.lambda().like(LcFlow::getDocType,docType);
        }
        IPage<LcFlow> flowIPage = flowMapper.selectPage(new Page<>(page,pageSize),queryWrapper);
        return flowIPage;
    }

    //修改流程
    @Override
    public int updateFlow(LcFlow flow) {
        return flowMapper.updateById(flow);
    }

    //添加流程
    @Override
    public int addFlow(LcFlow flow) {
        return flowMapper.insert(flow);
    }

    //检测流程名称是否合法
    @Override
    public boolean checkFlowName(Integer oid, String flowName) {
        QueryWrapper<LcFlow> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("flowName",flowName);
        LcFlow flow = flowMapper.selectOne(queryWrapper);
        boolean s = true;
        if(flow!=null){
            s = false;
            if (flow.getOid().equals(oid)) {
                s = true;
            }
        }
        return s;
    }

    //修改流程状态
    @Override
    public boolean editFlowState(Integer oid, String state) {
        Map<String, Object> map = new HashMap<>();
        map.put("oid",oid);
        map.put("state",state);
        /*启动流程-begin*/
        if(state.equals("Y")){
            //启动流程需要检测该流程是否有审批节点
            QueryWrapper<LcNode> wrapper = new QueryWrapper<>();
            wrapper.eq("parentid",oid);
            List<LcNode> nodes = nodeMapper.selectList(wrapper);
            if(!nodes.isEmpty()){
                //启动流程
                return flowMapper.editFlowState(map)>0;
            }
            return false;
        }
        /*启动流程-end*/
        /*禁用流程-begin*/
        if(flowMapper.editFlowState(map)>0){
            return true;
        };
        /*禁用流程-end*/
        return false;
    }

    //删除流程,只有当流程内没有节点了才能删除成功
    @Override
    public boolean deleteFlow(Integer oid) {
        boolean s = false;
        try {
           int row = flowMapper.deleteById(oid);
           if(row>0){
               s = true;
           }
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
        return s;
    }

}
